package cn.lili.controller.permission;

import cn.lili.common.enums.ResultUtil;
import cn.lili.common.security.AuthUser;
import cn.lili.common.security.context.UserContext;
import cn.lili.common.vo.PageVO;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.permission.entity.dos.Role;
import cn.lili.modules.permission.entity.vo.RoleVO;
import cn.lili.modules.permission.service.RoleService;
import cn.lili.mybatis.util.PageUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商家端,角色管理接口
 *
 * @author Chopper
 * @since 2020/11/20 18:50
 */
@RestController
@Api(tags = "管理端,角色管理接口")
@RequestMapping("/store/role")
public class RoleManagerController {
	@Autowired
	private RoleService roleService;

	@PostMapping
	@ApiOperation(value = "添加")
	public ResultMessage<Role> add(Role role) {
		roleService.save(role);
		return ResultUtil.data(roleService.updateRole(role));
	}

	@GetMapping
	@ApiOperation(value = "查询")
	public ResultMessage<Page> add(PageVO pageVo, Role role) {
		Page page = roleService.page(PageUtil.initPage(pageVo), PageUtil.initWrapper(role));
		return ResultUtil.data(page);
	}

	@ApiOperation(value = "查询全部角色列表")
	@RequestMapping(value = "/allChildren")
	public ResultMessage<List<RoleVO>> list(String state) {
		return ResultUtil.data(this.roleService.listAllChildren(state));
	}

	@PutMapping("/{roleId}")
	@ApiOperation(value = "编辑")
	public ResultMessage<Role> edit(@PathVariable String roleId, Role role) {
		role.setId(roleId);
		if (!role.getParentId().equals("0")) {
			Role byId = roleService.getById(role.getParentId());
			role.setAncestors(byId.getAncestors() + "," + role.getParentId());
		}
		roleService.updateById(role);
		return ResultUtil.data(role);
	}

	@DeleteMapping(value = "/{ids}")
	@ApiOperation(value = "批量删除")
	public ResultMessage<Role> delByIds(@PathVariable List<String> ids) {
		roleService.deleteRoles(ids);
		return ResultUtil.success();
	}

}
